/*
 * @Author: zhengzhuang
 * @since: 2019-12-24 16:47:20
 * @lastTime     : 2020-01-09 14:05:05
 * @LastAuthor   : zhengzhuang
 * @文件相对于项目的路径: /wangyueche_driver/lib/pages/intercity_car/intercity_pick_up_passengers.dart
 * @message: 城际出行去接乘客 IntercityPickUpPassengers
 */
import 'package:amap_map_fluttify/amap_map_fluttify.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:C20Driver/provider/order_information.dart';
import 'package:C20Driver/provider/position_information.dart';
import 'package:C20Driver/service/request.dart';
import 'package:C20Driver/utils/locate_info.dart';
import 'package:C20Driver/utils/permission_handler.dart';
import 'package:C20Driver/utils/utils.dart';
import 'package:provider/provider.dart';

class IntercityPickUpPassengers extends StatefulWidget {
  final String orderId;
  IntercityPickUpPassengers({this.orderId});
  @override
  _IntercityPickUpPassengersState createState() =>
      _IntercityPickUpPassengersState();
}

class _IntercityPickUpPassengersState extends State<IntercityPickUpPassengers> {
  AmapController _amapController;

  @override
  void initState() {
    super.initState();
    _getOrderInfo();
  }

  Future _getOrderInfo() async {
    await Request.driverGetIntercityOrder(widget.orderId, context);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
          child: Stack(
        children: <Widget>[
          _amap(),
          Positioned(
            bottom: 0,
            left: ScreenUtil().setWidth(30.0),
            child: _pickUpOperation(),
          ),
          // NoPassengersReceived(),
          SafeArea(
            child: Container(
                height: ScreenUtil().setWidth(80.0),
                padding: EdgeInsets.only(left: ScreenUtil().setWidth(10.0)),
                child: InkWell(
                  child: Icon(Icons.navigate_before,
                      size: ScreenUtil().setSp(60.0)),
                  onTap: () => Navigator.of(context).pop(),
                )),
          ),
        ],
      )),
    );
  }

  @override
  void dispose() {
    _amapController.dispose();
    // _amapController = null;
    super.dispose();
  }

  Widget _amap() {
    return Consumer<PositionInformation>(builder: (context, model, child) {
      return Container(
        height: double.infinity,
        width: ScreenUtil().setWidth(750.0),
        child: Column(
          children: <Widget>[
            Flexible(
              child: AmapView(
                showZoomControl: false,
                showCompass: true,
                // 中心点坐标
                centerCoordinate:
                    model.driverLocation != null ? model.driverLocation : null,
                // 缩放级别
                onMapCreated: (controller) async {
                  _amapController = controller;
                  if (await PermissionHandlerX.position(context)) {
                    var localtionInfo = await LocateInfo.getLocation(context);
                    if (localtionInfo == null) return;
                  }
                },
                onMapClicked: (latLng) {
                  return;
                },
              ),
            ),
          ],
        ),
      );
    });
  }

  /// 操作
  Widget _pickUpOperation() {
    return Container(
      width: ScreenUtil().setWidth(690.0),
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(ScreenUtil().setWidth(20.0)),
          boxShadow: <BoxShadow>[
            BoxShadow(
              color: Colors.black12,
              blurRadius: ScreenUtil().setWidth(10.0),
            ),
          ]),
      child: Column(
        children: <Widget>[
          SizedBox(height: ScreenUtil().setWidth(20.0)),
          Row(
            children: <Widget>[
              SizedBox(width: ScreenUtil().setWidth(20.0)),
              ClipOval(child:
                  Consumer<OrderInformation>(builder: (ctx, state, child) {
                return Image.network(
                  state.itineraryInfo.passengerHeadimgurl != null
                      ? state.itineraryInfo.passengerHeadimgurl
                      : '',
                  width: ScreenUtil().setWidth(100.0),
                  height: ScreenUtil().setWidth(100.0),
                );
              })),
              SizedBox(width: ScreenUtil().setWidth(20.0)),
              Container(
                width: ScreenUtil().setWidth(300.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Consumer<OrderInformation>(builder: (ctx, state, child) {
                      return Text(
                          '尾号${state.itineraryInfo.lastNum != null ? state.itineraryInfo.lastNum : ""}',
                          style: TextStyle(
                              fontSize: ScreenUtil().setSp(36.0),
                              color: Colors.black,
                              fontWeight: FontWeight.w700));
                    }),
                    Consumer<OrderInformation>(builder: (ctx, state, child) {
                      return Text(
                          '${state.itineraryInfo.depTime != null ? state.itineraryInfo.depTime : ""}  ${state.itineraryInfo.peopleNumber != null ? state.itineraryInfo.peopleNumber : ""}人',
                          style: TextStyle(
                              fontSize: ScreenUtil().setSp(36.0),
                              color: Color.fromARGB(255, 248, 169, 5),
                              fontWeight: FontWeight.w700));
                    }),
                  ],
                ),
              ),
            ],
          ),
          SizedBox(height: ScreenUtil().setWidth(20.0)),
          Divider(),
          SizedBox(height: ScreenUtil().setWidth(20.0)),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceAround,
            children: <Widget>[
              // Column(
              //   children: <Widget>[
              //     Icon(Icons.ac_unit, size: ScreenUtil().setSp(60.0)),
              //     Text('发消息',
              //         style: TextStyle(
              //             fontSize: ScreenUtil().setSp(24.0),
              //             color: Colors.black)),
              //   ],
              // ),
              Consumer<OrderInformation>(builder: (context, state, child) {
                return InkWell(
                    child: Column(
                      children: <Widget>[
                        Icon(Icons.near_me, size: ScreenUtil().setSp(60.0)),
                        Text('导航',
                            style: TextStyle(
                                fontSize: ScreenUtil().setSp(24.0),
                                color: Colors.black)),
                      ],
                    ),
                    onTap: () => Utils.openGaode(
                        lat: state.itineraryInfo.destLatitude.toString(),
                        lng: state.itineraryInfo.destLongitude.toString(),
                        name: state.itineraryInfo.destArea,
                        context: context));
              }),
              Consumer<OrderInformation>(builder: (context, state, child) {
                return InkWell(
                  child: Column(
                    children: <Widget>[
                      Icon(Icons.call, size: ScreenUtil().setSp(60.0)),
                      Text('打电话',
                          style: TextStyle(
                              fontSize: ScreenUtil().setSp(24.0),
                              color: Colors.black)),
                    ],
                  ),
                  onTap: () =>
                      Utils.callPhone(state.itineraryInfo.passengerMobile),
                );
              }),
            ],
          ),
          SizedBox(height: ScreenUtil().setWidth(50.0)),
          Consumer<OrderInformation>(builder: (context, state, child) {
            return _orderState(state.itineraryInfo.intercityStatus,
                state.itineraryInfo.orderId);
          }),
          SizedBox(height: ScreenUtil().setWidth(20.0)),
        ],
      ),
    );
  }

  /// 订单操作状态
  Widget _orderState(String state, String orderId) {
    print(state);
    if (state == '0040') {
      return MaterialButton(
          child: Text('去接乘客',
              style: TextStyle(
                  fontSize: ScreenUtil().setSp(36.0), color: Colors.white)),
          color: Color.fromARGB(255, 255, 103, 27),
          minWidth: ScreenUtil().setWidth(640.0),
          height: ScreenUtil().setWidth(100.0),
          onPressed: () => Request.pickUpPassengers(context, widget.orderId));
    } else if (state == '0045') {
      return MaterialButton(
          child: Text('到达乘客约定地点',
              style: TextStyle(
                  fontSize: ScreenUtil().setSp(36.0), color: Colors.white)),
          color: Color.fromARGB(255, 255, 103, 27),
          minWidth: ScreenUtil().setWidth(640.0),
          height: ScreenUtil().setWidth(100.0),
          onPressed: () => Request.driverArriveBoardingPointFun(
              context: context, orderId: orderId));
    } else if (state == '0055') {
      return MaterialButton(
          child: Text('乘客已上车',
              style: TextStyle(
                  fontSize: ScreenUtil().setSp(36.0), color: Colors.white)),
          color: Color.fromARGB(255, 255, 103, 27),
          minWidth: ScreenUtil().setWidth(640.0),
          height: ScreenUtil().setWidth(100.0),
          onPressed: () => Request.rintercityEceivePassenger(context, orderId));
    } else if (state == '0060') {
      return MaterialButton(
          child: Text('到达目的地',
              style: TextStyle(
                  fontSize: ScreenUtil().setSp(36.0), color: Colors.white)),
          color: Color.fromARGB(255, 255, 103, 27),
          minWidth: ScreenUtil().setWidth(640.0),
          height: ScreenUtil().setWidth(100.0),
          onPressed: () => Request.orderTripEndIntercity(
              orderId: orderId, context: context));
    }
    return Container();
  }
}
